package lvm

import (
	"context"
	"github.com/sirupsen/logrus"
)

// VGCreate 创建存储池
func VGCreate(ctx context.Context, vgName string, pvName string) (vg *VolumeGroup, err error) {
	// 前置条件检查
	if err = isPVJoinable(ctx, pvName); err != nil {
		logrus.Infof("pv %v not joinable", pvName)
		return
	}

	vgs, _ := VGList(ctx)
	if _, ok := vgs[vgName]; ok {
		err = ErrExists
		return
	}

	_, err = execCommand(ctx, "vgcreate", "-f", vgName, pvName)
	if err != nil {
		return
	}
	vgs, _ = VGList(ctx)
	if vg, ok := vgs[vgName]; ok {
		return vg, nil
	}
	return nil, ErrCommand
}
